perm filename RFNDIR[106,RWF] blob
sn#856246 filedate 1988-04-21 generic text, type C, neo UTF8
COMMENT ⊗ VALID 00006 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 \rm
C00004 00003 \rm
C00006 00004 @make(article)
C00009 00005 (****************************************************************************)
C00020 00006
C00021 ENDMK
C⊗;
\rm
\magnification=\magstep2
\def\today{\ifcase\month\or
January\or February\or March\or April\or May\or June\or
July\or August\or September\or October\or November\or December\fi
\space\number\day, \number\year}
\line{\sevenrm 106h4.tex[v1,rfn] \today\hfill}
\vskip .25in
\noindent
CS106H
\rightline{Autumn 1987}
\vskip .125in
\noindent
Assignment 4 (Reading, indefinite iteration)
\vskip .125in
Revise the stairway program to take from an input file the
number $n$ of steps and the height $h$ of each step. The
input file should look like
\hskip 15pt $n↓1\; h↓1\; n↓2\; h↓2\cdots n↓k\; h↓k\; 0$
\noindent
resulting in $k$ pictures. If you have problems, begin by
writing a program for which the input file contains only
$n↓1$ and $h↓1$.
Notice that the file contains one zero, not two.
\vfil\end
\rm
\magnification=\magstep2
\def\today{\ifcase\month\or
January\or February\or March\or April\or May\or June\or
July\or August\or September\or October\or November\or December\fi
\space\number\day, \number\year}
\line{\sevenrm 106h5.tex[v1,rfn] \today\hfill}
\vskip .25in
\noindent
CS106H
\rightline{Autumn 1987}
\vskip .25in
\noindent
Assignment 5 (Use of variables)
\vskip .25in
Your program should take integer values of $a$ and $b$ from an
input file, and compute the integers $c$ and $d$ for which
c + d\sqrt 5 = \bigl( a + b\sqrt 5\bigr) ↑i$ for $1≤i≤10$,
\noindent
printing $i$, $c$, and $d$.
\vskip .125in
\noindent
Hint: simplify the formula $\bigl( x + y\sqrt 5\bigr)
\bigl( u + v\sqrt 5\bigr)$.
\vfil\end
@make(article)
@begin(majorheading)
C.S. 106H Introduction to Computer Programming
@end(majorheading)
@begin(heading)
Autumn Quarter 1987-88
@end(heading)
@begin(description)
@b(Time):@\Mon. Wed. Fri., 10 AM
@b(Place):@\ Bldg. 550, Room 550D
@b(Instructor):@\ Robert W. Floyd
@\ SAIL Name: rwf@@su-ai
@\ LOTS Name: R.RWF
@\ Office: 342 Margaret Jacks Hall
@\ Office Hours: After class, or by appointment
@\ Phone: 723-1565 (Home 493-5195)
@b(Textbook):@\ @u(Required):
@\ @i(Notes on Programming in Pascal), R. W. Floyd
@\ @i(Standard Pascal), D. Cooper, or
@\ @i(The American Pascal), H. Ledgard.
@\ @i(LOTS DEC-System-20 Overview Manual)
@\ @u(Perhaps needed later in the quarter)
@\ @i(An Introduction to Programming and Problem Solving with Pascal),
@\ Schneider et al.
@\ @i(PASCAL and PASGO at LOTS), Rodriguez
@b(Course Work):@\ No exams. Homework will consist of programs to be
@\ written and tested at LOTS. In the beginning of the quarter we plan to
@\ have a short assignment due approximately every other class. As the
@\ quarter progresses, the assignments will become larger and spaced
@\ further apart, leading up to a final project. There will be about
@\ ten assignments altogether. Homework will be accepted after the due
@\ date only for medical reasons.
@b(Grades):@\ Your grade will depend solely on the total of your
@\ homework scores. The small assignments will probably be graded
@\ on a 0-10 scale, the medium ones 0-20, and the final project on a
@\ 0-40 scale.
@end(description)
(****************************************************************************)
(* CS106-3,6 Homework #7 *)
(* prof. R. Floyd Stanford Univ. *)
(* Author : Jerry Chu Nov. 12, 1982 *)
(*--------------------------------------------------------------------------*)
(* This program computes the value 'Pi' to 100 decimal places. By *)
(* changing the constant 'size', one can get more digits of accuracy. *)
(* The program has a debugger in it so that when the user was designing *)
(* the program, he can use the debugger to test if certain part of his *)
(* program is well done. *)
(****************************************************************************)
PROGRAM Pi(input:/,output);
LABEL
9999;
CONST
size = 20;
scale = 100000; (*the array type is an integer from 0 to scale*)
debug = true; (*specify whether to debug or not *)
TYPE
longreal = ARRAY[0..size] OF integer;
VAR
pi,arctan1,arctan2 : longreal;
numofterm,i : integer;
PROCEDURE Arrayadd(a,b : longreal; VAR c : longreal);
(* Add up two arrays a and b, put the result in c. *)
VAR
i,carry,sum : integer;
BEGIN
carry := 0;
FOR i := size DOWNTO 0 DO BEGIN
sum := a[i] + b[i] + carry;
c[i] := sum MOD scale;
carry := sum DIV scale;
END; (*for*)
IF carry > 0 THEN BEGIN
Writeln(' Overflow error stops ! (Real number exceeds',scale : 7,')');
GOTO 9999;
END; (*if*)
END; (*Arrayadd*)
PROCEDURE Arraysubt(a,b : longreal; VAR c : longreal);
(* Subtract b from a, put the result in c. *)
VAR
i,borrow : integer;
BEGIN
borrow := 0;
FOR i := size DOWNTO 0 DO BEGIN
IF a[i]-borrow >= b[i] THEN BEGIN
c[i] := a[i]-borrow-b[i];
borrow := 0;
END
ELSE BEGIN
c[i] := scale + a[i]-borrow-b[i];
borrow := 1;
END; (*else*)
END; (*for*)
IF borrow = 1 THEN BEGIN
Writeln('Underflow error stops ! (Real number less than 0)');
GOTO 9999
END; (*if*)
END; (*Arraysubt*)
PROCEDURE Arraydivision(a : longreal; b : integer; VAR c : longreal);
(* Divide a array by an integer b, put the result in c array. *)
VAR
i,remainder : integer;
BEGIN
IF b = 0 THEN BEGIN
Writeln('Divided by zero error !');
GOTO 9999;
END
ELSE BEGIN
remainder := 0;
FOR i := 0 TO size DO BEGIN
c[i] := (a[i]+remainder*scale) DIV b;
remainder := (a[i]+remainder*scale) MOD b;
END; (*for*)
IF remainder > (b DIV 2) THEN c[size] := c[size]+1;
END; (*else*)
END; (*Arraydivision*)
FUNCTION Checkzero(a : longreal) : boolean;
(* Check if the real number represented by array a is equal to zero.*)
VAR
i : integer;
zero : boolean;
BEGIN
zero := true;
i := 0;
WHILE zero AND (i <= size) DO BEGIN
zero := (a[i] = 0);
i := i+1;
END; (*while*)
Checkzero := zero;
END; (*Checkzero*)
PROCEDURE Write_longreal(a : longreal);
(* Write the value represented by array a.*)
VAR
i,j,k : integer;
FUNCTION Width(i : integer) : integer;
VAR
k : integer;
BEGIN
k := 0;
REPEAT
i := i DIV 10;
k := k+1;
UNTIL i = 0;
Width := k;
END; (*width*)
BEGIN (*Write_longreal*)
Write(a[0] : 5,'.');
k := Width(scale)-1;
FOR i := 1 TO size DO BEGIN
FOR j := 1 TO k-Width(a[i]) DO Write('0');
Write(a[i] : 1,' ');
END; (*for*)
Writeln;
END; (*Write_longreal*)
PROCEDURE Arctan(t: integer; VAR y : longreal; VAR n : integer);
(* Compute arctan(1/t) by the series given in the handout. Put the result *)
(* in y. When return, n equal to the number of terms being summed. *)
VAR
x,term : longreal; (* x holds the n-th power of (1/t). *)
(* term holds x/n. *)
BEGIN
x[0] := 1;
FOR i := 1 TO size DO x[i] := 0;
Arraydivision(x,t,x);
FOR i := 0 TO size DO BEGIN
term[i] := x[i];
y[i] := 0;
END; (*for*)
n := 0;
WHILE NOT Checkzero(term) DO BEGIN (* the terminating condition is when *)
(* the computed term equal to 0 *)
n := n+1;
IF (n MOD 2)=0 THEN Arraysubt(y,term,y)
ELSE Arrayadd(y,term,y);
Arraydivision(x,t,x);
Arraydivision(x,t,x);
Arraydivision(x,2*n+1,term);
END; (*while*)
END; (*Arctan*)
PROCEDURE Debugger;
(* Test several procedures using data given by the user from the terminal. *)
(* this procedure makes debugging of the program much easier. *)
VAR
i,j : integer;
a,b : longreal;
BEGIN
Writeln('Please give A and B initial values.');
Writeln('A :=');
Readln;
Read(a[0]);
Writeln('B :=');
Readln;
Read(b[0]);
FOR i := 1 TO size DO BEGIN
a[i] := 0;
b[i] := 0;
END;
i := 1;
WHILE i <> 0 DO BEGIN
Writeln('Please enter four choices of testing :');
Writeln(' 0 : quit; 1 : add; 2 : sub; 3 : div for A; 4 : div for B');
Readln;
Read(i);
CASE i OF
1 : BEGIN
Writeln('A <== A + B ');
Arrayadd(a,b,a);
Write_longreal(a);
END;
2 : BEGIN
Writeln('A <== A - B');
Arraysubt(a,b,a);
Write_longreal(a);
END;
3 : BEGIN
Writeln('Please enter divisor.');
Readln;
Read(j);
Writeln('A <== A / divisor');
Arraydivision(a,j,a);
Write_longreal(a);
END;
4 : BEGIN
Writeln('Please enter divisor.');
Readln;
Read(j);
Writeln('B <== B / divisor');
Arraydivision(b,j,b);
Write_longreal(b);
END;
OTHERS :
END; (*case*)
END; (*while*)
END; (*Debugger*)
BEGIN (*Main*)
IF debug THEN Debugger;
Writeln('The following computation uses the power series :');
Writeln(' 3 5 7 ');
Writeln(' arctan(x) = x - x /3 + x /5 - x /7 + .....');
Writeln('to compute Pi, the computed result has',5*size : 5,' accurate digits !');
Writeln;
numofterm := 0;
Arctan(239,arctan2,numofterm);
Writeln('ArcTan(1/239) =');
Write_longreal(arctan2);
Writeln('The number of terms being computed in the series is ',numofterm:1);
Writeln('-------------------------------------------------------------------------------');
Writeln;
Arctan(5,arctan1,numofterm);
Writeln('ArcTan(1/5) =');
Write_longreal(arctan1);
Writeln('The number of terms being computed in the series is ',numofterm:1);
Writeln('-------------------------------------------------------------------------------');
Writeln;
FOR i := 1 TO 2 DO Arrayadd(arctan1,arctan1,arctan1);
Arraysubt(arctan1,arctan2,pi);
FOR i := 1 TO 2 DO Arrayadd(pi,pi,pi);
Writeln('Pi =');
Write_longreal(pi);
9999 :
(*--------------------------------------------------------------------------*)
END. (*Main*)